package com.project8.book;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;

import com.project8.main.StickyNote;

public class Page implements Serializable 
{		
		private static final long serialVersionUID = -600472078577794348L;

		public static long numberOfPages = 1;
		
		long pageNum;
		String pageName;
		String pageNumDisplay;
		String contents;
		String contentUrl;  //Used for HTML pages in WebView
		long chapterID;
		String chapterName;
		Chapter chapter;
		boolean isPageBookmarked = false;		//Indicates whether this page has been bookmarked by the reader
		
		
		ArrayList<StickyNote> allStickies = new ArrayList<StickyNote>();
		ArrayList<String> figuresOnPage = new ArrayList<String>();
		ArrayList<String> figureCaptions = new ArrayList<String>();
		
		
		
		public Page(String name, Chapter chap)
		{
			pageName = name;
			pageNum = numberOfPages++;
			
			chapter = chap;
		}
		
		public long getPageNum()
		{
			return pageNum;
		}
		
		public void setPageNumDisplay(String displayedPageNum)
		{
			pageNumDisplay = displayedPageNum;
		}
		
		public String getPageNumDisplay()
		{
			return pageNumDisplay;
		}
	
		
		public void setPageText(String pageText)
		{
			contents = pageText;
		}
		
		/* Used for WebView -- url is the Url of the HTML page to display.  Should start with "file:///android_asset/" */
		public void setPageContent(String url)
		{
			contentUrl = url;
		}
		
		public void setChapterNum(long chID)
		{
			chapterID = chID;
		}
		
		public void setChapterName(String chName)
		{
			chapterName = chName;
		}
		
		public String getPageText()
		{
			return contents;
		}
		
		/* Used for WebView -- url is the Url of the HTML page to display.  Should start with "file:///android_asset/" */
		public String getPageContent()
		{
			return contentUrl;
		}
		
		public long getChapterNum()
		{
			return chapterID;
		}
		
		public String getChapterName()
		{
			return chapterName;
		}
		
		public Chapter getChapter()
		{
			return chapter;
		}
		
			
		@Override
		public boolean equals(Object o)
		{
			Page anotherPage = (Page)o;
			
			if(this.getPageNum() == anotherPage.getPageNum())
				return true;
			
			return false;
		}
		
		public void addStickyNoteToPage(StickyNote sticky)
		{
			allStickies.add(sticky);
		}
		
		public void removeStickyNoteFromPage(StickyNote sticky)
		{
			if(!allStickies.isEmpty())
			{
				allStickies.remove(sticky);
			}
		}
		
		public ArrayList<StickyNote> getAllStickyNotes()
		{
			return allStickies;
		}
		
		public boolean hasStickyNotes()
		{
			if(allStickies.size() == 0)
				return false;
			
			return true;
		}
		
		public StickyNote getNoteAtLocation(float xCoord, float yCoord)
		{
			StickyNote sticky;
			
			// Need to set a small range within your click to check for sticky note
			float minX = xCoord - 40;
			float maxX = xCoord + 40;
			float minY = yCoord - 40;
			float maxY = yCoord + 40;
					
			for(int s = 0; s < allStickies.size(); s++)
			{
				sticky = allStickies.get(s);
				
				if(sticky.getXCoordinate() > minX && sticky.getXCoordinate() < maxX && sticky.getYCoordinate() > minY && sticky.getYCoordinate() < maxY)
					return sticky;
			}
			
			return null;
		}
		
		public void bookmarkPage()
		{
			isPageBookmarked = true;
		}
		
		public void removeBookmark()
		{
			isPageBookmarked = false;
		}
		
		public boolean isBookmarked()
		{
			return isPageBookmarked;	
		}
		
		public String getPageName() {
			return pageName;
		}

		public void setPageName(String pageName) {
			this.pageName = pageName;
		}
		
		/*
		 *  Read in HTML Pages and Find every instance of image://
		 *  Return arrayList of figure filenames and ALT text
		 */
		public void findImages(InputStream html) throws IOException
		{
			InputStream mybuffer = html;
			DataInputStream datainput = new DataInputStream(mybuffer);

			while (datainput.available() != 0) {
				String line = datainput.readLine();
				
				if(line.lastIndexOf("image://") > 0)
				{
					String figure = line.substring(line.indexOf("image://") + 8, line.indexOf("\"", line.indexOf("image://") + 8));
					
					figuresOnPage.add(figure);
				}
				
				if(line.lastIndexOf("alt=\"") > 0)
				{
					String altText = line.substring(line.indexOf("alt=\"") + 5, line.indexOf("\"", line.indexOf("alt=\"") + 5));
					
					figureCaptions.add(altText);
				}
			}
			
		}
		
		public ArrayList<String> getFiguresOnPage() {
			return figuresOnPage;
		}

		public void setFiguresOnPage(ArrayList<String> figuresOnPage) {
			this.figuresOnPage = figuresOnPage;
		}
		
		public ArrayList<String> getFigureCaptions() 
		{
			return figureCaptions;
		}

		public void setFigureCaptions(ArrayList<String> figureCaptions) {
			this.figureCaptions = figureCaptions;
		}


}
